#include "spell.h"
#include "../affiliate/spriteAnima.h"

spell *spell::createSpell(node *parent, const char *path, glm::vec2 pos,float damage,
    const char* name,float scale, Anchor anchor)
{
    auto spell_ = new spell();
    spell_->init();
    spell_->name_ = name;   
    spell_->damage_ = damage;
    spell_->anima_ = spriteAnima::createAnima(spell_, path, scale, "spriteAnima",anchor);
    auto size = spell_->anima_->getSize();
    spell_->collider_ = collider::createCollider(spell_,size,Type::CIRCLE,
         "spellCollider", anchor);
    spell_->anima_->setLoop(false);
    spell_->setWorldPosition(pos);
    if(parent)parent->addChild(spell_);
    return spell_;
}

void spell::update(float dt)
{
    world::update(dt);
    if(anima_->isFinished())need_remove_ = true;
    attack();
}

void spell::attack(){
    auto ChildrenWorld = game_.getCurrentScene()->getChildrenWorld();
    for(auto& node_ : ChildrenWorld){
        if(node_->getType()!= nodeType::ENEMY)continue;
        if(collider_ && node_->getCollider() && collider_->isColliding(node_->getCollider())){
            //TODO
            node_->takeDamage(damage_);
        }
    }
}